package com.my.study.jdbc.dao.impl;

import com.my.study.jdbc.dao.AccountDao;
import com.my.study.jdbc.entity.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/11/6 13:25
 **/
@Repository
public class AccountDaoImpl implements AccountDao {

    @Resource(name = "jdbcTemplateOne")
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(Account account) {
        String sql = "INSERT INTO t_account(user_name, money) VALUES (?, ?)";
        return jdbcTemplate.update(sql, account.getUserName(), account.getMoney());
    }

    @Override
    public int update(Account account) {
        String sql = "UPDATE t_account SET user_name = ? WHERE id = 1";
        return jdbcTemplate.update(sql, account.getUserName());
    }

    @Override
    public int updateMoneyById(Account account) {
        String sql = "UPDATE t_account SET money = ? WHERE id = ?";
        return jdbcTemplate.update(sql, account.getMoney(), account.getId());
    }

    @Override
    public void addMoney(Account account) {
        String sql = "UPDATE t_account SET money = money + ? WHERE user_name = ?";
        jdbcTemplate.update(sql, account.getMoney(), account.getUserName());
    }

    @Override
    public void reduceMoney(Account account) {
        String sql = "UPDATE t_account SET money = money - ? WHERE user_name = ?";
        jdbcTemplate.update(sql, account.getMoney(), account.getUserName());
    }
}
